<?php

namespace app\admin\controller;

use app\admin\model\RsBrand;
use app\admin\model\RsCate;
use app\common\model\Export;
use app\model\CouponGoods;
use app\model\RsRelate;
use think\facade\Db;

class Resources extends Controller
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     *  资源管理--商品分类.
     */
    public function cateList()
    {
        $model = new RsCate();
        $data = input('post.');

        return $model->cateList($data);
    }

    //删除分类
    public function delCate()
    {
        $model = new RsCate();
        $data = input('post.');

        return $model->delCate($data);
    }

    //设置分类
    public function optionCate()
    {
        $model = new RsCate();
        $data = input('post.');

        return $model->optionCate($data);
    }

    //设置状态
    public function optionStatus()
    {
        $model = new RsCate();
        $data = input('post.');

        return $model->optionStatus($data);
    }

    /**
     *  资源管理--品牌列表.
     */
    public function brandList()
    {
        $model = new RsBrand();
        $data = input('post.');

        return $model->brandList($data);
    }

    // 返回品牌列表选项
    public function relateBrand()
    {
        $model = new RsRelate();
        $data = input('post.');

        return $model->brandList($data);
    }

    // 修改排序
    public function changeSort()
    {
        $model = new RsRelate();
        $data = input('post.');

        return $model->changeSort($data);
    }

    //增加品牌关联
    public function doRelate()
    {
        $model = new RsRelate();
        $data = input('post.');

        return $model->doRelate($data);
    }

    //设置关联品牌状态
    public function relateStatus()
    {
        $model = new RsRelate();
        $data = input('post.');

        return $model->relateStatus($data);
    }

    //删除关联
    public function delRelate()
    {
        $model = new RsRelate();
        $data = input('post.');

        return $model->delRelate($data);
    }

    //返回添加关联品牌时下拉选项
    public function brandSelect()
    {
        $model = new RsBrand();
        $data = input('post.');

        return $model->brandSelect($data);
    }

    //设置状态
    public function brandStatus()
    {
        $model = new RsBrand();
        $data = input('post.');

        return $model->optionStatus($data);
    }

    //设置品牌
    public function optionBrand()
    {
        $model = new RsBrand();
        $data = input('post.');

        return $model->optionBrand($data);
    }

    /**
     * 上传品牌图标.
     */
    public function uploadBrand()
    {
        $model = new RsBrand();

        return $model->uploadBrand();
    }

    /**
     *  获取代理产品类型.
     */
    public function agentServType()
    {
        $data = getServTypeKeyMap();

        return success('操作成功', $data);
    }

    /**
     *   导入excel商品
     */
    public function importGoods()
    {
        $model = new Export();
        $data = $model->importExcel();
        if (!is_array($data)) {
            return $data;
        }
        $cateId = $brandId = $type = '';
        Db::startTrans();
        try {
            foreach ($data as $nk => $nv) {
                //添加分类
                if (false !== strpos($nv[0], '类目')) {
                    $cateId = RsCate::importCate(trim($nv[0]));
                } else {
                    if (!empty($nv[0])) {
                        $info = isset($nv[10]) ? $nv[10] : '';
                        $brandId = RsBrand::importBrand($nv[0], $cateId, $info);
                        $type = $nv[1];
                    }
                    CouponGoods::importGoods($nv, $brandId, $type);
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();

            return fail('导入失败：' . $e->getMessage());
        }

        return  success('导入成功！');
    }

    /**
     * 商品分类
     */
    public function showCate()
    {
        $model = new RsCate();
        return $model->showCate();
    }

    /**
     * 充值类型
     */
    public function rechargeType()
    {
        $brandId = input('post.brand_id');
        if (empty($brandId)) {
            return fail('品牌 id 不能为空');
        }
        $model = new CouponGoods();
        $list = $model->rechargeType($brandId);
        return success('操作成功', $list);
    }

    /**
     * 充值面额
     */
    public function rechargeAmount()
    {
        $brandId = input('post.brand_id');
        if (empty($brandId)) {
            return fail('品牌 id 不能为空');
        }
        $typeName = input('post.type_name');
        if (empty($typeName)) {
            return fail('充值类型不能为空');
        }
        $model = new CouponGoods();
        $list = $model->rechargeAmount($brandId, $typeName);
        return success('操作成功', $list);
    }

    /**
     * 修改商品排序
     */
    public function changeGoodsSort()
    {
        $model = new CouponGoods();
        $data = input('post.');

        $res = $model->changeSort($data);
        return success('修改成功');
    }

    /**
     * 修改商品上下架状态
     */
    public function changeGoodsShelfState()
    {
        $model = new CouponGoods();
        $data = input('post.');

        $res = $model->changeShelfState($data);
        return success('修改成功');
    }
}
